package com.example.tabledemo;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Paint;
import android.media.audiofx.AudioEffect;
import android.os.Build;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {



    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiLine();
        //intiPie();
        //intiBar();
    }


    private void intiLine(){
        LineChart mLineChart=findViewById(R.id.chart1);
        //无背景网格
        mLineChart.setDrawGridBackground(false);
        mLineChart.setDrawBorders(false);
        //图标描述
        Description description=new Description();
        //描述内容
        description.setText("折线图");
        //描述字体颜色
        description.setTextColor(0xff000000);
        //描述字体大小
        description.setTextSize(24f);
        //文字左对齐
        description.setTextAlign(Paint.Align.LEFT);
        //设置图标描述
        description.setPosition(100,100);
        mLineChart.setDescription(description);
        //获取坐标轴并进行设置
        //获取X轴对象
        XAxis xAxis=mLineChart.getXAxis();
        //设置显示X轴
        xAxis.setEnabled(true);
        //X轴位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
       //设置X轴宽度
        xAxis.setAxisLineWidth(2);
        //无网格
        xAxis.setDrawGridLines(false);
        //显示X轴
        xAxis.setDrawAxisLine(true);
        //x轴数据
        final String[] xValues={"3.14","3.15","3.16","3.17","3.18","3.19","3.20"};
        //给x轴设置数据
        IndexAxisValueFormatter formatter=new IndexAxisValueFormatter();
        formatter.setValues(xValues);
        xAxis.setValueFormatter(formatter);

        //设置Y轴
        YAxis leftYAxis=mLineChart.getAxisLeft();
        YAxis rightYAxis=mLineChart.getAxisRight();
        rightYAxis.setEnabled(false);
        leftYAxis.setAxisLineWidth(2);
        leftYAxis.setDrawGridLines(false);
        leftYAxis.setStartAtZero(true);

        //添加数据
        List<Entry> entries1=new ArrayList<>();
        entries1.add(new Entry(1,85));
        entries1.add(new Entry(2,75));
        entries1.add(new Entry(3,65));
        entries1.add(new Entry(4,89));
        entries1.add(new Entry(5,37));
        entries1.add(new Entry(6,82));
        List<Entry> entries2=new ArrayList<>();
        entries2.add(new Entry(0,75));
        entries2.add(new Entry(1,26));
        entries2.add(new Entry(2,58));
        entries2.add(new Entry(5,89));
        entries2.add(new Entry(6,37));
        entries2.add(new Entry(7,78));

        LineDataSet lineDataSet1=new LineDataSet(entries1,"Line1");
        lineDataSet1.setColor(0xff3f51b5);

        LineDataSet lineDataSet2=new LineDataSet(entries2,"Line2");
        lineDataSet1.setColor(0xffff4081);

        List<ILineDataSet> dataSets=new ArrayList<>();
        dataSets.add(lineDataSet1);
        dataSets.add(lineDataSet2);

        LineData lineData=new LineData(dataSets);
        mLineChart.setData(lineData);
        mLineChart.invalidate();


    }

   /*@RequiresApi(api = Build.VERSION_CODES.M)
    private void intiPie(){
        PieChart pieChart=findViewById(R.id.chart2);
        List<PieEntry> list=new ArrayList<>();
        list.add(new PieEntry(30f,"aaa"));
        list.add(new PieEntry(70f,"bbb"));

        PieDataSet data=new PieDataSet(list,"Label");
        data.setColors(getColor(R.color.teal_700),getColor(R.color.purple_200));

        PieData pieData=new PieData(data);
        pieData.setDrawValues(true);
        pieChart.setData(pieData);
        pieChart.invalidate();
        //去掉右下角label
        Description description=new Description();
        description.setText("");
        pieChart.setDescription(description);
        //空心变实心
        //半径设为0
        pieChart.setHoleRadius(0f);
        //内圈此时为半透明，设置半透明的半径为0
        pieChart.setTransparentCircleRadius(0f);


    }*/

    /*private void intiBar(){
        BarChart barChart=findViewById(R.id.chart3);
        barChart.getDescription().setEnabled(true);
        Description description=new Description();
        //description.setText("柱状图统计");
        description.setTextSize(14f);
        barChart.setDescription(description);
        barChart.setNoDataText("没有数据");
        barChart.setDrawGridBackground(false);
        barChart.setDrawValueAboveBar(true);
        barChart.setPinchZoom(true);
        barChart.setDrawBarShadow(false);
        barChart.setDrawGridBackground(false);
        barChart.setDoubleTapToZoomEnabled(false);
        barChart.animateY(1500);

        List<BarEntry> list=new ArrayList<BarEntry>();
        String[] xVals={"数据1","数据2","数据3"};
        BarEntry barEntry1=new BarEntry(0,10);
        BarEntry barEntry2=new BarEntry(1,35);
        BarEntry barEntry3=new BarEntry(2,17);
        //xVals.add("数据1");
        list.add(barEntry1);
        list.add(barEntry2);
        list.add(barEntry3);

        XAxis xAxis=barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        IndexAxisValueFormatter formatter=new IndexAxisValueFormatter();
        formatter.setValues(xVals);
        xAxis.setValueFormatter(formatter);
        xAxis.setLabelCount(xVals.length);
        BarDataSet dataSet=new BarDataSet(list,"柱状图");
        List<Integer> color=new ArrayList<Integer>();
        color.add(Color.BLUE);
        color.add(Color.CYAN);
        color.add(Color.GREEN);
        //color.add(Color.rgb(44,120,221));
        dataSet.setColors(color);
        List<IBarDataSet> dataSets=new ArrayList<IBarDataSet>();
        dataSets.add(dataSet);
        BarData barData=new BarData(dataSets);
        barData.setDrawValues(true);
        barData.setValueTextColor(Color.BLACK);
        barData.setValueTextSize(13);
        barChart.setData(barData);
        barChart.animateXY(800,800);
        barChart.setVisibleXRangeMaximum(15);
        barChart.invalidate();
        description.setText("");
        barChart.setDescription(description);

    }*/
}